# Build script for binutils.
#
# Copyright (c) 2014-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version=2.38

cd -- "$TMPDIR"
rm -rf binutils-${version}
unpack "${worktree}/sources/binutils-${version}.tar.lz"

# Build instructions
cd binutils-${version}

# Apply patches from Fedora (Nick Clifton)

patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-export-demangle.h.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-no-config-h-check.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-filename-in-error-messages.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-revert-PLT-elision.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-readelf-other-sym-info.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-2.27-aarch64-ifunc.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-link-with-static-libstdc++.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-special-sections-in-groups.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-mismatched-section-flags.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-CVE-2019-1010204.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-warn-unsupported.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-use-long-long.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gold-i386-gnu-property-notes.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-autoconf-version.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/gcc12-libtool-no-rpath.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-section-type.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-gas-loc-view.patch"
patch -Np1 -i "${worktree}/patches/binutils/2.38/binutils-do-not-use-debuginfod.patch"

patch -Np1 -i "${worktree}/patches/binutils/branch-updates.diff"

# Build in a separate directory
rm -rf ../binutils-build
mkdir ../binutils-build
cd ../binutils-build

../binutils-${version}/configure \
AR="ar" AS="as" CC="$BTCC" CXX="$BTCXX" \
CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
 --prefix="$crossdir" \
 --libdir="${crossdir}/lib${libSuffix}" \
 --host=$host \
 --target=$target \
 --enable-deterministic-archives \
 --enable-separate-code \
 --disable-compressed-debug-sections \
 --disable-generate-build-notes \
 --disable-werror \
 --disable-nls \
 --disable-ppl-version-check \
 --disable-cloog-version-check \
 --with-sysroot="${crossdir}/$target" \
 $multilib_options

make -j${jobs} V=1 MAKEINFO="true"
make -j${jobs} V=1 MAKEINFO="true" install

cd -- "$TMPDIR"

cleanup()
{
    rm -rf binutils-${version} binutils-build
}

